package cn.com.hatech.cdkey.module.generate.controller;

import cn.com.hatech.cdkey.common.controller.BaseController;
import cn.com.hatech.cdkey.module.generate.entity.CdKey;
import cn.com.hatech.cdkey.module.generate.service.ICdKeyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.cdkey.module.generate.controller
 * @className: GenerateController
 * @author: xuruikai
 * @description: 生成激活码控制器
 * @date: 2020年2月27日 16:00:52
 * @version: 1.0
 */
@Controller
@Slf4j
@RequestMapping("/cd/key")
public class CdKeyController extends BaseController {

    private final ICdKeyService cdKeyService;

    @Autowired
    public CdKeyController(ICdKeyService cdKeyService) {
        this.cdKeyService = cdKeyService;
    }

    /**
     * 页面跳转
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "cdkey/index";
    }

    /**
     * 页面跳转
     *
     * @return
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        request.setAttribute("allTenant", cdKeyService.selectAllTenant());
        return "cdkey/add";
    }

    /**
     * 页面跳转
     *
     * @return
     */
    @RequestMapping("/batch")
    public String batch() {
        return "cdkey/batch";
    }

    /**
     * 生成记录
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit) {
        QueryWrapper<CdKey> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        request.setAttribute("list", cdKeyService.page(new Page<>(page, limit), queryWrapper));
        return "cdkey/list";
    }
}
